/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.ui.forms.DkFixedFormPanel;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.SupplierDetailPropertyAccess;
import com.daikit.daikit4gxtsample.client.screen.testscreen.TestScreen;
import com.daikit.daikit4gxtsample.client.ui.field.SupplierDetailEditorGrid;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.google.gwt.editor.client.Editor;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.form.FieldLabel;


/**
 * Second tab of {@link TestScreen} editor
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class ProductEditorTab2 extends AbstractEditorTab implements Editor<ProductData>
{

	private final FieldLabel specialPropertyFieldLabel;

	private final SupplierDetailEditorGrid supplierDetailsGrid2;
	private final SupplierDetailEditorGrid supplierDetailsGrid3;

	/**
	 * Constructor
	 * 
	 * @param controller
	 */
	public ProductEditorTab2(final ProductEditorController controller)
	{
		super(controller);
		final TabItemConfig tabItemConfig = new TabItemConfig();
		tabItemConfig.setIcon(Main.icons().table_16());
		tabItemConfig.setText(Main.gwtSampleI18n().test_screen_editor_tab_2_title());
		setTabItemConfig(tabItemConfig);


		supplierDetailsGrid2 = new SupplierDetailEditorGrid(new ListStore<SupplierDetailData>(
				SupplierDetailPropertyAccess.INSTANCE.key()))
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}

			@Override
			public void forceLayout()
			{
				// TODO Auto-generated method stub
				super.forceLayout();
			}

			@Override
			protected void doLayout()
			{
				// TODO Auto-generated method stub
				super.doLayout();
			}
		};
		//		supplierDetailsGrid2.setVisible(false);
		supplierDetailsGrid2.setHeadingHtml("BITE");


		supplierDetailsGrid3 = new SupplierDetailEditorGrid(new ListStore<SupplierDetailData>(
				SupplierDetailPropertyAccess.INSTANCE.key()))
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}

			@Override
			public void forceLayout()
			{
				// TODO Auto-generated method stub
				super.forceLayout();
			}
		};
		//		supplierDetailsGrid3.setVisible(false);
		supplierDetailsGrid3.setHeadingHtml("CUL");

		final FieldLabel property1FieldLabel = new FieldLabel(getEditor().property1, Main.gwtSampleI18n().product_property1());
		final FieldLabel property2FieldLabel = new FieldLabel(getEditor().property2, Main.gwtSampleI18n().product_property2());
		specialPropertyFieldLabel = new FieldLabel(getEditor().productSpecialSubTypeEditor.getEditor().special, Main
				.gwtSampleI18n().product_special_property());

		final DkFixedFormPanel myFormPanel = new DkFixedFormPanel(2);

		myFormPanel.add(property1FieldLabel, 0);
		myFormPanel.add(property2FieldLabel, 1);
		myFormPanel.add(specialPropertyFieldLabel, 0);
		myFormPanel.add(supplierDetailsGrid2);
		myFormPanel.add(supplierDetailsGrid3);

		add(myFormPanel);
	}

	@Override
	public void invalidateUi()
	{
		getTabItemConfig().setEnabled(getEditor().isEditing());
		getEditor().property1.setEnabled(getEditor().isEditing());
		getEditor().property2.setEnabled(getEditor().isEditing());
		getEditor().productSpecialSubTypeEditor.getEditor().special.setEnabled(getEditor().isEditing());

		final boolean isSpecialProductData = getEditor().isSpecialProductData();
		specialPropertyFieldLabel.setVisible(isSpecialProductData);


		final boolean visible2 = supplierDetailsGrid2.isVisible();
		final boolean visible3 = supplierDetailsGrid3.isVisible();
		final boolean newVisible = getEditor().isEditing();
		if (visible2 != newVisible || visible3 != newVisible)
		{
			supplierDetailsGrid2.setVisible(newVisible);
			supplierDetailsGrid3.setVisible(newVisible);
			getController().getProductEditorUI().getTabPanel().forceLayout();
		}

	}

	@Override
	public boolean needToBeReloaded()
	{
		return true;
	}

	@Override
	public DkBaseAction<?> getReloadSubPanelAction(final boolean force, final Object... optionalArgs)
	{
		return null;
	}
}
